﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BYS.XiAnComDept.BussLogic
{
    public static class IEnumerableExtension
    {
        public static List<T> Merge<T>(this IEnumerable<IEnumerable<T>> seperatedCollection, 
            Func<List<T>, T, bool> checkDuplicate = null)
        {
            List<T> result = new List<T>();
            foreach (IEnumerable<T> collections in seperatedCollection)
            {
                foreach (T t in collections)
                {
                    if (checkDuplicate == null)
                    {
                        checkDuplicate = (x, y) => x.Contains(y);
                    }
                    if (checkDuplicate(result, t))
                    {
                        result.Add(t);
                    }
                }
            }
            return result;
        }

        public static bool IsSameCollection<T>(this IEnumerable<T> my, IEnumerable<T> other)
        {
            if (my.Count() != other.Count())
            {
                return false;
            }

            // any element of my but not contains at other?
            if (my.Count(x => !other.Contains(x)) > 0)
            {
                return false;
            }

            return true;
        }
    }
}
